# coding=utf-8
__author__ = 'st316'


class Solution:
    # @return a int
    def minCost(self, warehouse):
        n = len(warehouse) - 1  # 仓库数目
        s = [0 for i in xrange(n)]
        total = sum(warehouse)
        average = float(total) / n  # 计算每个仓库应该最终存储的值
        # print total, average
        for i in xrange(1, n):  # 计算sum数组
            s[i] = warehouse[i] + s[i - 1] - average
        print s
        s.pop(0)  # 去掉第一个为0的无用元素
        s.sort()  # 排序后打算取中位数
        print s
        mid = s[(n - 1) / 2]
        print mid
        ans = abs(mid)
        for i in xrange(0, n - 1):
            ans += abs(s[i] - mid)
        return ans


if __name__ == '__main__':
    s = Solution()
    print s.minCost([0, 1, 2, 3, 4, 5, 6, 7])